package com.ruoyi.work.service.impl;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.work.domain.WorkFill;
import com.ruoyi.work.domain.vo.WorkDataClearRecord;
import com.ruoyi.work.mapper.WorkDataClearMapper;
import com.ruoyi.work.mapper.WorkFillMapper;
import com.ruoyi.work.service.IWorkDataClearService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @Author: MMDZ
 * @Desc:
 */
@Service
public class WorkDataClearServiceImpl implements IWorkDataClearService {
    @Autowired
    private WorkDataClearMapper workDataClearMapper;
    @Autowired
    private WorkFillMapper workFillMapper;

    @Override
    public TableDataInfo selectFillDataList(WorkDataClearRecord workDataClearRecord, Integer pageNum, Integer pageSize) {
        int defaultDay = selectDictValueById();
        PageUtils.startPage(pageNum, pageSize);//设置数据分页
        return PageUtils.getDataTable(workDataClearMapper.selectFillDataList(
                defaultDay, workDataClearRecord.getNickName(), workDataClearRecord.getState()));
    }


    @Override
    public TableDataInfo selectClearDataList(WorkDataClearRecord workDataClearRecord, Integer pageNum, Integer pageSize) {
        PageUtils.startPage(pageNum, pageSize);//设置数据分页
        return PageUtils.getDataTable(workDataClearMapper.selectClearDataList(workDataClearRecord));
    }

    @Override
    public WorkDataClearRecord selectDataClearById(Long clearId) {
        return workDataClearMapper.selectDataClearById(clearId);
    }

    @Override
    @Transactional
    public int edit(WorkDataClearRecord workDataClearRecord) {
        if ("1".equals(workDataClearRecord.getIsRestore())) {
            // 已还原
            return 1;
        }
        // 获取历史数据
        WorkDataClearRecord record = workDataClearMapper.selectDataClearById(workDataClearRecord.getClearId());
        if (Objects.isNull(record)) {
            throw new ServiceException("历史数据为空！清除失败");
        }
        // 还原数据
        WorkFill fill = workFillMapper.selectWorkFillById(record.getFillId());
        if (Objects.nonNull(fill)) {
            throw new ServiceException("数据已存在！");
        }
        WorkFill workFill = new WorkFill();
        BeanUtils.copyProperties(record, workFill);
        workFill.setCreateBy(SecurityUtils.getNickname());
        workFillMapper.insertWorkFill(workFill);
        // 改变状态
        workDataClearRecord.setFillId(workFill.getFillId());
        workDataClearRecord.setUpdateBy(SecurityUtils.getNickname());
        workDataClearRecord.setIsRestore("1");
        return workDataClearMapper.edit(workDataClearRecord);
    }

    /**
     * 获取字典中默认天数
     *
     * @return
     */
    @Override
    public int selectDictValueById() {
        String defaultDay = workDataClearMapper.selectDictValueById(29l);
        return StringUtils.isNotEmpty(defaultDay) ? Integer.parseInt(defaultDay) : 7;
    }

    @Override
    @Transactional
    public int delete(WorkDataClearRecord workDataClearRecord) {
        // 获取填报数据
        WorkFill workFill = workFillMapper.selectWorkFillById(workDataClearRecord.getFillId());
        if (Objects.isNull(workFill)) {
            throw new ServiceException("填报数据为空！清除失败");
        }
        // 已删除填报数据 存入历史表（查看历史表是否已有该记录）
        workDataClearRecord.setClearBy(SecurityUtils.getNickname());
        workDataClearRecord.setCreateBy(SecurityUtils.getNickname());
        workDataClearMapper.insert(workDataClearRecord);
        // 删除填报数据
        return workFillMapper.deleteWorkFillById(workDataClearRecord.getFillId());
    }
}
